package cn.com.surker.handler;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.base.system.service.SysLineService;
import cn.com.surker.enums.production.invoice.InvoiceCreateTypeEnum;
import cn.com.surker.enums.production.invoice.InvoiceProcessEnum;
import cn.com.surker.production.entity.vo.DispatchVo;
import cn.com.surker.production.entity.vo.InvoiceHandleDetail;
import cn.com.surker.production.entity.vo.ProductionInvoiceVo;
import cn.com.surker.production.entity.vo.TranCarVo;
import cn.com.surker.production.service.ErpCarService;
import cn.com.surker.production.service.ErpIndentDispatchService;
import cn.com.surker.production.service.ErpInvoiceHandleService;
import cn.com.surker.production.service.ErpInvoiceService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 调度查询功能
 *
 * @author luotao
 * @date 2025/4/2
 */
@Component
public class DisCoordinator {

	//	生产线key
	public static final String lingProducingKey = "PRO_DIS_LING_PRODUCING_KEY";
	//	正在生产的发货单数据
	public static final String disInvKey = "PRO_DIS_PROING_INV_KEY";
	//	全部的的车辆
	public static final String allCarKey = "PRO_DIS_ALL_CAR_LIST_KEY";
	//	退砼车辆
	public static final String backInvKey = "PRO_DIS_BACK_INV_KEY";
	//	正供任务单id
	public static final String workIndentKey = "PRO_DIS_WORK_INDENT_KEY";

	//	任务单的累计方量和车次,使用时,后面要根任务单的id
	public static final String accuAndTranKey = "PRO_DIS_ACCU_AND_TRAN";
	//	近10车
	public static final String nearlyInvKey = "PRO_DIS_NEARLY_INV_KEY";

	@Value("${erp.org.id}")
	private Long orgId;

	@Value("${erp.datasource.erp.db}")
	private String erpDb;

	@Resource
	private SysLineService sysLineService;
	@Resource
	protected ErpIndentDispatchService indentDispatchService;
	@Resource
	private ErpCarService tranCarService;
	@Autowired
	protected ErpInvoiceHandleService invoiceHandleService;
	@Resource
	protected ErpInvoiceService invoiceService;


	/**
	 * 查询生产线的派单信息
	 */
	public ArrayList<Map<String, Object>> lingProducing() {
		//	查询生产线
		List<SysLine> lines = sysLineService.findList(null);
		//	查询派单信息
		List<DispatchVo> dispatchList = indentDispatchService.findFullList(null, null, null, InvoiceProcessEnum.PRO.getCode(), InvoiceProcessEnum.QUERN_PRO.getCode(), InvoiceProcessEnum.QUERN_RECEIVE.getCode());
		Map<Long, List<DispatchVo>> groupData = null;
		if (dispatchList != null && dispatchList.size() > 0) {
			//	根据生产线分组
			groupData = dispatchList.stream().collect(Collectors.groupingBy(DispatchVo::getLineId));
		}
		//	返回数据
		ArrayList<Map<String, Object>> resList = new ArrayList<>();
		for (SysLine line : lines) {
			Map<String, Object> res = new HashMap<>();
			//	生产线信息
			Long lineId = line.getId();
			String lineName = line.getName();
			//	派单车辆
			List<DispatchVo> disList = null;
			if (groupData != null) {
				disList = groupData.get(lineId);
			}
			res.put("lineId", lineId);
			res.put("lineName", lineName);
			res.put("disList", disList);
			resList.add(res);
		}
		return resList;
	}

	/**
	 * 车辆列表
	 *
	 * @return
	 */
	public List<TranCarVo> carList() {
		List<TranCarVo> list = tranCarService.findOrgList(orgId, null, "sign_time");
		return list;
	}

	/**
	 * 近10车的退砼
	 *
	 * @return
	 */
	public List<InvoiceHandleDetail> nearlyBackList() {
		IPage<InvoiceHandleDetail> backPage = invoiceHandleService.findInvBackPage(erpDb, null, null, null, null, null, null, 0, 0, 0, 10);
		List<InvoiceHandleDetail> records = null;
		if (backPage != null) {
			records = backPage.getRecords();
		}
		return records;
	}

	/**
	 * 用于调度页面查看的正在生产的方量
	 *
	 * @return
	 */
	public List<ProductionInvoiceVo> disInvList() {
		List<Integer> processList = new ArrayList<>();
		processList.add(InvoiceProcessEnum.QUERN_RECEIVE.getCode());
		processList.add(InvoiceProcessEnum.QUERN_PRO.getCode());
		processList.add(InvoiceProcessEnum.PRO.getCode());
		IPage<ProductionInvoiceVo> page = invoiceService.selectFullPageV2(erpDb, null, null, null, null, null,
			InvoiceCreateTypeEnum.AUTOMATIC.getCode(), processList, 0, 50);
		List<ProductionInvoiceVo> records = page.getRecords();
		//	进行排序
		if (records != null && records.size() > 0) {
			records.sort(Comparator.comparing(
				ProductionInvoiceVo::getMixName,
				Comparator.nullsFirst(Comparator.naturalOrder())
			).thenComparingInt((ProductionInvoiceVo vo) -> InvoiceProcessEnum.PRO.getCode().equals(vo.getProcess()) ? -1 : 0)
				.thenComparing(ProductionInvoiceVo::getCreateTime));
		}
		return records;
	}

	/**
	 * 近10车发货单
	 *
	 * @return
	 */
	public List<ProductionInvoiceVo> nearlyInvList() {
		List<Integer> processList = new ArrayList<>();
		processList.add(InvoiceProcessEnum.PRO_COMPLETE.getCode());
		processList.add(InvoiceProcessEnum.TRAN.getCode());
		processList.add(InvoiceProcessEnum.TRAN_COMPLETE.getCode());
		IPage<ProductionInvoiceVo> page = invoiceService.selectFullPageV2(erpDb, null, null, null, null, null,
			InvoiceCreateTypeEnum.AUTOMATIC.getCode(), processList, 1, 10);
		List<ProductionInvoiceVo> records = null;
		if (page != null) {
			records = page.getRecords();
		}
		return records;
	}

	/**
	 * 任务单的累计车次和方量
	 *
	 * @param indentId
	 * @return
	 */
	public HashMap<String, Object> accuAndTran(Long indentId) {
		//	先查询累计车次和方量
		Map<String, Object> totalMap = invoiceService.queryTotalStere(erpDb, indentId);
		if (totalMap == null) {
			totalMap = new HashMap<>();
		}
		Object totalCar = totalMap.get("totalCar");
		Object totalStere = totalMap.get("totalStere");
		List<String> carCodeList = invoiceService.findTranCar(erpDb, indentId);
		HashMap<String, Object> res = new HashMap<>();
		res.put("totalCar", totalCar);
		res.put("sendStere", totalStere);
		res.put("carCodeList", carCodeList);
		return res;
	}

}
